/*
 * @Author: lineCode 1145938682@qq.com
 * @Date: 2025-01-22 22:09:13
 * @LastEditors: lineCode 1145938682@qq.com
 * @LastEditTime: 2025-02-18 22:58:30
 * @FilePath: /backend/shorthand-modules/shorthand-modules-content/src/main/java/com/angus/modules/content/repository/CNContentRepository.java
 */
package com.angus.modules.content.repository;

import com.angus.modules.content.domain.CNContent;
import com.angus.modules.content.mapper.CNContentMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liyanan
 * @date 2024/10/21 5:27 PM
 * @description
 */
@Repository
public class CNContentRepository extends ServiceImpl<CNContentMapper, CNContent> {
    public List<CNContent> selectContentByGroupID(String courseCode, Long groupID) {
        return  baseMapper.selectList(Wrappers.lambdaQuery(CNContent.class)
                .eq(CNContent::getGroupId, groupID)
                .ne(CNContent::getGroupFlag, groupID+"-s")
                .eq(CNContent::getCourseCode, courseCode)
                .eq(CNContent::getDelFlag, 0)
                .orderByDesc(CNContent::getGroupId));
    }

    public List<CNContent> selectWordByGroupFlag(List<String> groupFlags, String courseCode) {
        return  baseMapper.selectList(Wrappers.lambdaQuery(CNContent.class)
                .in(CNContent::getGroupFlag, groupFlags)
                .eq(CNContent::getCourseCode, courseCode)
                .eq(CNContent::getDelFlag, 0));
    }

    public List<CNContent> selectContentList(String courseCode, Integer unitNum) {
        return baseMapper.getContentList(courseCode,unitNum);
    }

    public Long selectNotInIds(List<Long> ids) {
        return baseMapper.selectCount(Wrappers.lambdaQuery(CNContent.class)
                .notIn(CNContent::getId, ids)
                .eq(CNContent::getDelFlag, 0));
    }

    public List<CNContent> getByCourseCode(String courseCode) {
        return baseMapper.selectList(Wrappers.lambdaQuery(CNContent.class)
                .eq(CNContent::getCourseCode, courseCode)
                .eq(CNContent::getDelFlag, 0));
    }

    public List<CNContent> selectContentByGroupId(String courseCode, Integer unitNum, Long groupId) {
        return baseMapper.selectContentByGroupId(courseCode,unitNum,groupId);
    }

    public CNContent selectContentByGroupFlag(String courseCode, Integer unitNum, String groupFlag) {
        return baseMapper.selectContentByGroupFlag(courseCode,unitNum,groupFlag);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean updateSortToId(){
        int rows = baseMapper.updateSortToId();
        return rows > 0;
    }
}
